package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/4/3 13:09
* @Version : 1.0
* @Description : 预约设置表现层接口实现类
*/
@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 上传文件
     * @param orderSettingList
     */
    @Override
    public void upload(List<OrderSetting> orderSettingList) {
        //判断集合数据是否为空
        if (orderSettingList==null){
            throw new RuntimeException("数据不能为空");
        }
        //遍历集合 判断执行方法
        for (OrderSetting orderSetting : orderSettingList) {
            //判断指定日期是否存在
            Long count= orderSettingDao.countByOrderDate(orderSetting);
            //根据count的值执行对应方法
            if (count>0){
                orderSettingDao.updateOrderSetting(orderSetting);
            }else {
                orderSettingDao.add(orderSetting);
            }
        }
    }

    /**
     * 指定月份的预约数据
     * @param date
     * @return
     */
    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {
        //获取指定月份的初始日期
        String dateStar= date+"-1";
        //获取指定月份的最后日期
        String dateEnd=date+"-31";
        //将以上两个数据存入map集合中
        HashMap<String, String> map = new HashMap<>();
        map.put("dateStar",dateStar);
        map.put("dateEnd",dateEnd);
        //调用dao 方法 获取指定月份数据
        List<OrderSetting> orderSettingList= orderSettingDao.getOrderSettingByMonth(map);
        //转换成前端需要的格式
        List<Map<String, Integer>> list = new ArrayList<>();
        //遍历获取的数据集合
        for (OrderSetting orderSetting : orderSettingList) {
            //定义一个Map<String, Integer>, 存当前预约设置的信息
            Map<String, Integer> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            list.add(orderSettingMap);
        }
        return list;
    }

    /**
     * 根据日期 修改预约设置数据
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断数据是否存在
        Long count= orderSettingDao.countByOrderDate(orderSetting);
        if (count>0){
            orderSettingDao.updateOrderSetting(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
